import 'package:cbec_app/components/decoration/decoration_ad_hot_area.dart';
import 'package:cbec_app/components/decoration/decoration_carousel_product.dart';
import 'package:cbec_app/components/decoration/decoration_goods.dart';
import 'package:cbec_app/components/decoration/DecorationGoodsGroup/decoration_goods_group.dart';
import 'package:cbec_app/components/decoration/decoration_image_text_nav.dart';
import 'package:cbec_app/components/decoration/DecorationCutDownActivity/decoration_kill_sale.dart';
import 'package:cbec_app/components/decoration/decoration_magic_cube.dart';
import 'package:cbec_app/components/decoration/decoration_notice.dart';
import 'package:cbec_app/components/decoration/DecorationRedPacket/decoration_red_packet.dart';
import 'package:cbec_app/components/decoration/decoration_rich_text_parse.dart';
import 'package:cbec_app/components/decoration/decoration_search_input.dart';
import 'package:cbec_app/components/decoration/decoration_slider_group.dart';
import 'package:cbec_app/components/decoration/DecorationSpecialSuperProduct/decoration_special_super_product.dart';
import 'package:cbec_app/components/decoration/decoration_title_06_text.dart';
import 'package:cbec_app/components/decoration/decoration_title_text.dart';
import 'package:cbec_app/components/decoration/decoration_topic_product.dart';
import 'package:cbec_app/components/decoration/decoration_video.dart';
import 'package:cbec_app/components/decoration/decoration_white_line.dart';
import 'package:cbec_app/components/decoration/decortaion_ad_image.dart';
import 'package:cbec_app/components/decoration/DecorationNewZone/docoration_new_zone.dart';
import 'package:cbec_app/entity/home/content/advance_sale_data.dart';
import 'package:cbec_app/entity/home/content/bargain_activity_data.dart';
import 'package:cbec_app/entity/home/content/cateslide_group_entity.dart';
import 'package:cbec_app/entity/home/content/content_data.dart';
import 'package:cbec_app/entity/home/content/goods_data_model.dart';
import 'package:cbec_app/entity/home/content/groupon_activity_data.dart';
import 'package:cbec_app/entity/home/content/scroll_image_slider_entity.dart';
import 'package:cbec_app/entity/home/content/sec_kill_data.dart';
import 'package:cbec_app/entity/home/content/special_price_data.dart';
import 'package:cbec_app/entity/home/content/superior_special_price_entity.dart';
import 'package:cbec_app/entity/home/content/topic_product_entity.dart';
import 'package:cbec_app/pages/home/controller/home_ctr.dart';
import 'package:cbec_app/pages/home/controller/secondary_micro_ctr.dart';
import 'package:cbec_app/pages/signIn/SignInWidget.dart';
import 'package:cbec_app/utils/logger.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../components/decoration/DecorationCoupon/decoration_coupon.dart';
import '../../../entity/home/content/com_title06_entity.dart';
import '../../../entity/home/content/coupon_decoration_entity.dart';
import '../../../entity/home/content/group_goods_data_entity.dart';
import '../../../entity/home/content/red_packet_decoration_entity.dart';
import '../../../entity/home/content/text_title_entity.dart';
import '../../lottery/view/SimpleLotteryWidget.dart';

class DecorationRenderer extends StatelessWidget {
  final bool isSecondaryMicro;
  const DecorationRenderer({super.key, this.isSecondaryMicro = false});

  @override
  Widget build(BuildContext context) {
    if (isSecondaryMicro) {
      return GetBuilder<SecondaryMicroCtr>(
        builder: (controller) {
          // 使用 SecondaryMicroCtr 的 contentComponents
          final List<Widget> components = generateComponents(controller.contentComponents, controller);
          // logger.i('生成的组件有: $components');
          if (components.isEmpty) {
            return const SizedBox();
          }
          return Column(
            children: components,
          );
        },
      );
    } else {
      return GetBuilder<HomeController>(
        builder: (controller) {
          // 使用 HomeController 的 contentComponents
          final List<Widget> components = generateComponents(controller.contentComponents, controller);
          logger.i('生成的组件有: $components');
          if (components.isEmpty) {
            return const SizedBox();
          }
          return Column(
            children: components,
          );
        },
      );
    }
  }

  // 通用的组件生成方法
  List<Widget> generateComponents(List<ContentData> content, dynamic controller) {
    // print('-------> ${content.map((item)}');
    return content.map((item) {
      // print('每一个 ${item.toString()}');
      switch (item.type) {
        case 'title_text':
          return DecorationTitleText(info: item as TextTitleEntity);
        case 'rich_text':
          return DecorationRichTextParse(info: item.toJson());
        case 'white_line':
          return DecorationWhiteLine(info: item.toJson());
        case 'new_image_ad':
          return DecorationAdImage(info: item.toJson());
        case 'image_ad_hot_area':
          return DecorationAdHotArea(info: item.toJson());
        case 'cube':
          return DecorationMagicCube(info: item.toJson());
        case 'search':
          return DecorationSearchInput(info: item.toJson());
        case 'image_text_nav':
          return DecorationImageTextNav(info: item.toJson());
        case 'video':
          return DecorationVideo(info: item.toJson());
        case 'notice':
          return DecorationNotice(info: item.toJson());
        case 'goods':
          final goodsDataModel = item as GoodsDataModel;
          return DecorationGoods(info: item.toJson(), goodsList: goodsDataModel.goods!.map((good) => good.toJson()).toList());
        case 'goods_group':
          final goodsGroupData = item as GroupGoodsDataEntity;
          DecorationGoodsGroup decorationGoodsGroup = DecorationGoodsGroup(info: item.toJson(), idKey: 'commodityId', goodsType: 0);
          return decorationGoodsGroup;
        case 'new_zone':
          return DecorationNewZone(info: item.toJson());
        case 'ump_limitdiscount': // 特价
          final specialPriceData = item as SpecialPriceData;
          return DecorationKillSale(
            info: item.toJson(),
            idKey: 'goodsId',
            curPriceKey: 'seckillPrice',
            originPriceKey: 'price',
            goodsType: 1,
          );

        case 'ump_seckill': // 秒杀
          final secKillData = item as SeckillData;
          return DecorationKillSale(
            info: item.toJson(),
            idKey: 'goodsId',
            curPriceKey: 'seckillPrice',
            originPriceKey: 'price',
            goodsType: 3,
          );
        case 'bargain':
          final bargainActivity = item as BargainActivity;
          return DecorationKillSale(
            info: item.toJson(),
            idKey: 'goodsId',
            curPriceKey: 'seckillPrice',
            originPriceKey: 'price',
            goodsType: 4,
          );
        case 'groupon':
          final grouponActivity = item as GrouponActivity;
          return DecorationKillSale(
            info: item.toJson(),
            idKey: 'goodsId',
            curPriceKey: 'seckillPrice',
            originPriceKey: 'price',
            goodsType: 2,
          );

        case 'advance_sale':
          final advanceSaleContent = item as AdvanceSaleContent;
          return DecorationKillSale(
            info: item.toJson(),
            idKey: 'goodsId',
            curPriceKey: 'seckillPrice',
            originPriceKey: 'price',
            goodsType: 5,
          );
        case 'comtitle06':
          final comTitle06Model = item as ComTitle06Entity;
          logger.i('comTitle06Model === ${comTitle06Model.toJson()}');
          return Decoration06TitleText(model: comTitle06Model);
        case 'cateslide': // 滑动分组组件
          final sliderGroupModel = item as CateslideGroupEntity;
          return DecorationSliderGroup(model: sliderGroupModel);

        case 'topicitems': // 主题商品A1
          final topicProductData = item as TopicProductEntity;
          logger.i('topicProductData === ${topicProductData.toJson()}');
          return DecorationTopicProduct(model: topicProductData);
        case 'limitdiscount01': // 特价组件A1
          return DecorationSpecialSuperProduct(model: item as SuperiorSpecialPriceEntity);
        case 'imgslide01': // 轮播
          final scrollImageSliderData = item as ScrollImageSliderEntity;
          logger.i('scrollImageSliderData === ${scrollImageSliderData.toJson()}');
          return DecorationCarouselProduct(model: scrollImageSliderData);
        case 'raffle':
          return SimpleLotteryWidget(
              controller: controller.simpleLotteryController,
              info: controller.lotteryBean,
              lotteryListBean: controller.lotteryListBean,
              onPress: () {
                controller.requestWin();
                // controller.showResultDialog();
              },
              onPressList: () {
                controller.requestLotteryList();
              },
              mapParam: item.toJson());

        case 'signin':
          return SignInWidget(
              signInData: controller.signInRecoredBean,
              onPress: () {
                controller.requestSignInResult();
                // controller.showSignInResultDialog();
              },
              mapParam: item.toJson());

        case 'red_packet':
          final redPacketDecorationEntity = item as RedPacketDecorationEntity;
          return DecorationRedPacket(model: redPacketDecorationEntity);

        case 'coupon':
          final couponDecorationEntity = item as CouponDecorationEntity;
          return DecorationCoupon(model: couponDecorationEntity);

        default:
          return const SizedBox.shrink(); // 默认返回空组件
      }
    }).toList();
  }
}
